<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$find = MiscUtils::getParam('find', '');
$cat = MiscUtils::getParam('cat', 0);
$from = MiscUtils::getParam('from', '');
$to = MiscUtils::getParam('to', '');

$cond_vals = new stdClass(); 
$cond_vals->c = '';
$cond_vals->v = array();
$cond_vals->c = "(t.order_number LIKE :v1 OR t.first_name LIKE :v1 OR t.last_name LIKE :v1 OR t.phone LIKE :v1 OR t.email LIKE :v1)";
$cond_vals->v[":v1"] = "%$find%";

if ($cat == 1) {
  $cond_vals->c .= " AND t.is_paid = :v2";
  $cond_vals->v[":v2"] = 0;
} else if ($cat == 2) {
  $cond_vals->c .= " AND t.is_paid = :v2 AND t.is_delivered = 0";
  $cond_vals->v[":v2"] = 1;
} else if ($cat == 3) {
  $cond_vals->c .= " AND t.is_delivered = :v2 AND date_deliver >= '" . date('Y-m-d', time() - 864000) . "'";
  $cond_vals->v[":v2"] = 1;
} else if ($cat == 4) {
  $cond_vals->c .= " AND t.is_delivered = :v2 AND date_deliver < '" . date('Y-m-d', time() - 864000) . "'";
  $cond_vals->v[":v2"] = 1;
}

if ($cat == 5) {
  $cond_vals->c .= " AND t.is_cancel = 1";
} else {
  $cond_vals->c .= " AND t.is_cancel = 0";
}

if ($from != '' && $to != '') {
  $cond_vals->c .= " AND (t.date >= :v3 AND t.date <= :v4)";
  $cond_vals->v[":v3"] = $from;
  $cond_vals->v[":v4"] = $to;
} else if ($from != '' && $to == '') {
  $cond_vals->c .= " AND t.date >= :v3";
  $cond_vals->v[":v3"] = $from;
} else if ($from == '' && $to != '') {
  $cond_vals->c .= " AND t.date <= :v3";
  $cond_vals->v[":v3"] = $to;
}

$SIZE = 50;
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.id');
$queue = MiscUtils::getParam('q', 'DESC');
$items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, $order, $queue, $page, $SIZE, NULL);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #666;
  border-top: 1px solid #666;
  font-size: 12px;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #222;
  padding: 0 5px; 
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #fff;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
.imgbtn {
  background-image: url(images/add.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
.imgico {
  cursor: pointer;
}
.searchwrapper {
  width:310px; /*follow your image's size*/
  height:40px;/*follow your image's size*/
  background-image:url(images/searchbox.jpg);
  background-repeat:no-repeat; /*important*/
  padding:0px;
  margin:0px;
  position:relative; /*important*/
}
.searchbox {
  border:0px; /*important*/
  background-color:transparent; /*important*/
  position:absolute; /*important*/
  top:4px;
  left:9px;
  width:256px;
  height:31px;
  color:#fff;
  outline: none;
}
.searchbox_submit {
  border:0px; /*important*/
  background-color:transparent; /*important*/
  position:absolute; /*important*/
  top:4px;
  left:265px;
  width:32px;
  height:32px;
}
.styled-select {
  width: 240px;
  height: 34px;
  overflow: hidden;
  background: url(images/down_arrow_select.jpg) no-repeat right #ddd;
  border: 1px solid #ccc;
  margin-left: 5px;
}
.styled-select select {
  background: transparent;
  width: 268px;
  padding: 5px;
  font-size: 16px;
  line-height: 1;
  border: 0;
  border-radius: 0;
  height: 34px;
  -webkit-appearance: none;
}
.styled-select option {
  padding: 5px;
  font-size: 16px;
  line-height: 1;
}
.drwrap {
  background: #ddd;
  border: 1px solid #666;
  height: 34px;
  margin-left: 5px;
  line-height: 32px;
  vertical-align: middle;
  font-family: Lucida Sans Unicode;
}
.drwrap input[type=text]{
  border: 1px solid #aaa;
  height: 23px;
  padding-left: 5px;
}
.imgbtn {
  background-image: url(images/ok.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
  width: 80px;
  margin-bottom: 5px;
}
.imgbtn:hover {
  background-color: #eee;
}
.cancel {
  background-image: url(images/cancel.png) !important;
  margin-left: 5px;
}
</style>
</head>
<body>
<div>
  <table cellPadding=0 cellSpacing=0>
    <tr>
      <td>
        <div class="searchwrapper">
          <input type="text" class="searchbox" id="s_kw" value="<?php echo $find; ?>" onkeypress="enter(event);"/>
          <input type="image" src="images/blank.png" class="searchbox_submit" value="" onclick="find();"/>
        </div>
      </td>
      <td>
        <div class="drwrap">
          &nbsp;From&nbsp;<input id="s_from" type="text" value="<?php echo $from; ?>"></input>&nbsp;to&nbsp;<input id="s_to" type="text" value="<?php echo $to; ?>"></input>&nbsp;
        </div>
      </td>
      <td>
        <div class="styled-select">
          <select id="s_cat" onchange="find();">
            <option>All Orders</option>
            <option>Unpaid Only</option>
            <option>Requires shipping</option>
            <option>Shipped in last 10 days</option>
            <option>Shipped after 10 days</option>
            <option>Cancelled</option>
          </select>
        </div>
      </td>
    </tr>
  </table>
</div>
<div>&nbsp;</div>
<div>
  <table class="list" style="margin-top: 5px;" cellPadding="0" cellSpacing="0">
    <tr>
      <th class="sort" onclick="set_order('date')" width="60px">Date</th>
      <th class="sort" onclick="set_order('order_number')"  width="60px">Order Number</th>
      <th class="sort" onclick="set_order('is_paid')" width="40px">Paid</th>
      <th class="sort" onclick="set_order('is_delivered')"  width="40px">Shipped</th>
      <th class="sort" onclick="set_order('date_deliver')"  width="60px">Ship Date</th>
      <th class="sort" onclick="set_order('comment')" width="250px">Comment</th>
      <th class="sort" onclick="set_order('total')" width="60px">Total</th>
      <th class="sort" onclick="set_order('first_name')" width="80px">First Name</th>
      <th class="sort" onclick="set_order('last_name')" width="80px">Last Name</th>
      <th class="sort" onclick="set_order('phone')" width="80px">Phone</th>
      <th class="sort" onclick="set_order('email')" width="200px">Email</th>
      <th class="sort" onclick="set_order('payment_name')" width="80px">Payment</th>
      <th class="sort" onclick="set_order('delivery_name')" width="80px">Delivery</th>
      <th class="sort" width="90px"></th>
    </tr>
    <?php foreach ($items->d as $idx => &$item) { ?>
    <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
      <td><?php echo $item->date; ?></td>
      <td><?php echo $item->order_number; ?></td>
      <td><input type="checkbox" onclick="update_attr('<?php echo $item->id; ?>', 'is_paid', this.checked ? 1 : 0);" <?php echo (($item->is_paid == 1) ? 'checked="checked"' : ''); ?>></input></td>
      <td><input id="shipped_<?php echo $item->id; ?>" type="checkbox" onclick="update_attr('<?php echo $item->id; ?>', 'is_delivered', this.checked ? 1 : 0);" <?php echo (($item->is_delivered == 1) ? 'checked="checked"' : ''); ?>></input></td>
      <td><?php if ($item->is_delivered == 1) echo $item->date_deliver; ?></td>
      <td><?php echo MiscUtils::getShortName($item->comment, 40); ?></td>
      <td>$<?php echo $item->total; ?></td>
      <td><?php echo $item->first_name; ?></td>
      <td><?php echo $item->last_name; ?></td>
      <td><?php echo $item->phone; ?></td>
      <td><?php echo $item->email; ?></td>
      <td><?php echo $item->payment_name; ?></td>
      <td><?php echo $item->delivery_name; ?></td>
      <td>
        <img title="Print" src="images/print.png" class="imgico" onclick="window.open('http://<?php echo URL . 'exts/plugins/admin_order/print_order.php?id=' . $item->order_number; ?>');"></img>
        <img title="View" src="images/view.png" class="imgico" onclick="window.open('http://<?php echo URL . 'incls/php/ecomorder.print.php?secret_pass=0&id=' . $item->order_number; ?>');"></img>
        <img title="Edit" src="images/edit.png" class="imgico" onclick="parent.location.href=parent.document.URL + '&script=index_order&id=<?php  echo $item->id; ?>';"></img>
        <?php if ($item->is_cancel == 0) { ?>
        <img title="Cancel" src="images/delete24.png" class="imgico" onclick="cancel_order('<?php echo $item->order_number; ?>');"></img>
        <?php } else { ?>
        <img title="Restore" src="images/restore.png" class="imgico" onclick="restore_order('<?php echo $item->order_number; ?>');"></img>
        <?php } ?>
      </td>
    </tr>
    <?php } ?>
    <?php if (count($items->d) == 0) { ?>
    <tr class="none"><td colspan=14>No Results</td></tr>
    <?php } ?>
  </table>
  <div id="pagination"></div>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>
<script src="../../../libs/jquery_ui/js/jquery-ui-1.10.2.custom.js"></script>
<link href="jquery_ui/css/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet">
<script type="text/javascript" src="pop/pop.js"></script>
<script type="text/javascript">
  function set_order(col) {
    parent.location.href='http://<?php echo URL. "admin.php?i=$page_id&p=$items->p&q=" . (($items->q == 'ASC') ? 'DESC' : 'ASC'); ?>&o=' +  col + '&find=' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + encodeURIComponent($('#s_to')[0].value);
  }

  function update_attr(id, attr, value) {
    if (attr == 'is_delivered' && value == 1) {
      var p = new Pop({
        p: MyUtils.findPos($('#shipped_' + id)[0]),
        w: 250
      });
      var div, elm;
      div = document.createElement('div');
      div.style.textAlign = 'center';
      p._content.appendChild(div);
      elm = document.createElement('b');
      elm.appendChild(document.createTextNode('Shipping Date'));
      div.appendChild(elm);
      elm = document.createElement('input');
      elm.type = 'text';
      elm.style.width = '95%';
      elm.id = 'shipping_date';
      elm.value = '<?php echo date('Y-m-d', time()); ?>'
      elm.style.border = '1px solid #ddd';
      elm.style.margin = '5px 0';
      elm.style.height = '25px';
      elm.style.font = ''
      elm.style.textAlign = 'center';
      div.appendChild(elm);
      $("#shipping_date").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: "yy-mm-dd",
        showButtonPanel: true,
      });
      elm = document.createElement('input');
      elm.type = 'submit';
      elm.value = 'OK';
      elm.className = 'imgbtn';
      elm.onclick = function() {
        $.ajax({
          type: "POST",
          url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
          data: 'action=update_attr&attr=' + attr + '&id=' + id + '&value=' + value + '&date=' + $("#shipping_date")[0].value,
          success: function(msg){
            location.reload();
          }
        });
        p.close();
      };
      div.appendChild(elm);
      elm = document.createElement('input');
      elm.type = 'submit';
      elm.value = 'Cancel';
      elm.className = 'imgbtn cancel';
      elm.onclick = function() {
        $('#shipped_' + id)[0].checked = 0;
        p.close();
      };
      div.appendChild(elm);

      
      
    } else {
      $.ajax({
        type: "POST",
        url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
        data: 'action=update_attr&attr=' + attr + '&id=' + id + '&value=' + value,
        success: function(msg){
          location.reload();
        }
      });
    }
  }
  
  function cancel_order(id) {
    r = window.confirm('Are You Sure?');
    if (!r) {
      return;
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=cancel_order&id=" + encodeURIComponent(id),
      success: function(msg){
        location.reload();
      }
    });
  }

  function restore_order(id) {
    r = window.confirm('Are You Sure?');
    if (!r) {
      return;
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=restore_order&id=" + encodeURIComponent(id),
      success: function(msg){
        location.reload();
      }
    });
  }

  function print_order(id) {
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=print_order&id=" + encodeURIComponent(id),
      success: function(msg){
        location.reload();
      }
    });
  }
  
  function enter(ev) {
    if (window.event) {
      key = window.event.keyCode;
      isShift = window.event.shiftKey ? true : false;
    } else {
      key = ev.which;
    }
    if (key != 13) {
      return;
    }
    find();
  }
  
  function find() {
    var str = 'http://<?php echo URL . "admin.php?i=$page_id&find="; ?>' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + encodeURIComponent($('#s_to')[0].value);
    parent.location.href = str;
  }

  $(function(){
    MyUtils.cleanDatepicker();
    $("#s_from").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&find="; ?>' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + dateText + '&to=' + encodeURIComponent($('#s_to')[0].value);
      }
    });
    $("#s_to").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&find="; ?>' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + dateText;
      }
    });
    var elm = document.getElementById('pagination');
    MyUtils.removeChildElements(elm);
    
    $('#s_cat')[0].selectedIndex = <?php echo $cat; ?>;
    
    new ModulePagination(document, elm, null, null, null, null, {
      page: <?php echo $items->p; ?>,
      total: <?php echo $items->t; ?>,
      limit: <?php echo $items->s; ?>,
      callbackFunc: function(page) { 
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&o=$items->o&q=$items->q"; ?>&p=' + page + '&find=' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + encodeURIComponent($('#s_to')[0].value);
      },
      contenxt: this
    });
  });
</script>
</body>
</html>